package com.ys.controller.web.business;

import com.bo.vo.W;
import com.ys.entity.business.TpProductBasis;
import com.ys.entity.business.TpProductBasisComponent;
import com.ys.entity.business.TpProductMaterial;
import com.ys.entity.sys.TsUserChange;
import com.ys.enume.business.TpProductMaterialEnum;
import com.ys.service.sys.CommonApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/business/tpProductBasis")
public class WebTpProductBasisController {
	
	private static Long MENU_ID = 27L;
	
	@Autowired
	private CommonApiService<TpProductBasis> commonService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			return commonService.toAdd(model, new TpProductBasis(), MENU_ID, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑页
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){

		try {
			Map entity = new TpProductBasis().setId(id)
					.addLeftJoin("TB_BaseinfoSppl D ON  D.id = A.CommodityPlId")
					.addLeftJoin("TB_BaseinfoDwmc G ON  G.id = A.NameUnitId")
					.addLeftJoin("TB_BaseinfoSppm W ON  W.id = A.NameId")
					.addLeftJoin("TB_BaseinfoSpyt H ON  H.id = A.UsageId")
					.addLeftJoin("TB_BaseinfoLmwh O ON  O.id = A.CommodityLmId")
					.addLeftJoin("TB_BaseinfoLmwh Q ON  Q.id = A.CateOneJPId")
					.addLeftJoin("THR_OperateCompanyBeian U ON  U.Code = A.VgsBm")
					.addField("U.FullName AS vgsMc,U.Name AS vgsJc")
					.addField("D.categoryCn AS commodityPl")
					.addField("G.NameUnitCn AS nameUnit")
					.addField("W.NameCn AS nameCn,W.NameEn AS nameEn ")
					.addField("H.UsageCn AS usageCn,H.UsageEn AS usageEn ")
					.addField("O.CategoryPath AS vspLm1")
					.addField("Q.CategoryPath AS cateOneJp")
					.queryFirstMap();

			List<TpProductMaterial> tpProductMaterialList = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductBasis.value).where("A.ProductCode = '" + entity.get("code").toString() + "'").queryList();

			if (tpProductMaterialList.size() != 0){
				// 处理商品材质列表
				StringBuilder materialCnList = new StringBuilder();
				StringBuilder materialEnList = new StringBuilder();
				StringBuilder materialIdList = new StringBuilder();
				for (int i = 0; i < tpProductMaterialList.size(); i++) {
					materialCnList.append(","+tpProductMaterialList.get(i).getMaterialNameCn());
					materialEnList.append(","+tpProductMaterialList.get(i).getMaterialNameEn());
					materialIdList.append(","+tpProductMaterialList.get(i).getId());
				}
				entity.put("materialCn",materialCnList.substring(1,materialCnList.length()));
				entity.put("materialEn",materialEnList.substring(1,materialEnList.length()));
				entity.put("materialId",materialIdList.substring(1,materialIdList.length()));
			}

			if(entity.get("createChangeId")!=null){
				TsUserChange tsUserChange = new TsUserChange().setId(Long.valueOf(entity.get("createChangeId").toString())).setStatus(1).queryFirst();

				entity.put("createUser",tsUserChange.getUserName() );
				entity.put("createDeptName",tsUserChange.getDeptName() );
				entity.put("createPosition",tsUserChange.getRoleName() );
			}

			commonService.toEdit(model, id, new TpProductBasis(), MENU_ID, entity, null);

			List<TpProductBasisComponent> listTpProductBasisComponent = new TpProductBasisComponent()
					.setPcode(entity.get("code").toString())
					.setOrderby("A.Code asc")
					.queryList();
			model.addAttribute("sonlist", listTpProductBasisComponent);
			model.addAttribute("pcode", entity.get("code").toString());
			model.addAttribute("onProcess", 1);

			return "business/tpProductBasis/toEdit";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
}
